function [aaa]=scatter_plots(proj_meta,cur_site)

order={};
exp_inds={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
    if length(nbr_frames)>5
        for xnd=1:length(nbr_frames)
            if nbr_frames(xnd)==5000
                if xnd==length(nbr_frames)
                    order{tnd}(xnd)=3;
                else
                    order{tnd}(xnd)=1;
                end
            else
                order{tnd}(xnd)=2;
            end
        end
        ord_ind=1:length(order{tnd});
        order{tnd}(max(ord_ind(order{tnd}==2))+1:end)=3;
    else
        order{tnd}=[1 2 1 2 3];
    end
    exp_inds{tnd}(1,1)=1;
    exp_inds{tnd}(1,2)=nbr_frames(1);
    for fnd=2:length(order{tnd})
        exp_inds{tnd}(fnd,1)=exp_inds{tnd}(fnd-1,2)+1;
        exp_inds{tnd}(fnd,2)=sum(nbr_frames(1:fnd));
    end
end

shad_all={};
vis_fl_all={};
% delta_all={};
pert_num={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    cur_pert_num=[];
    ps_id=proj_meta(cur_site).rd(1,tnd).ps_id;
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.01;
    runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.01;
%     pupil_pos=proj_meta(cur_site).rd(1,tnd).pupil_pos;
    shad=[];
    vis_fl=[];
    cnt=1;
    for bnd=1:length(order{tnd})
        if order{tnd}(bnd)==1
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if tmp_runM(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==0 %feedback
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==1 %feedback mismatch
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        elseif order{tnd}(bnd)==2
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_runP=runP(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=5;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=6;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    shad(cnt)=2;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0
                    shad(cnt)=3;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        end
    end
    shad_all{tnd}=shad;
    vis_fl(length(vis_fl):length(runM))=0;
    vis_fl_all{tnd}=vis_fl;
%     delta=sqrt(diff(pupil_pos(1,:)).^2+diff(pupil_pos(2,:)).^2);
%     delta(end+1)=delta(end);
%     delta_all{tnd}=delta;
    pert_num{tnd}=cur_pert_num;
end

all_act={};
pl_cells=[];
mean_fluo=[];
for tnd=1:size(proj_meta(cur_site).rd,2)
    cnt=0;
    for vnd=1:size(proj_meta(cur_site).rd,1)
        all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
        mean_fluo(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),tnd)=mean(proj_meta(cur_site).rd(vnd,tnd).act,2);
        cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
        pl_cells(vnd)=cnt;
    end
end

fb_snip={};
pb_snip={};
fb_av={};
pb_av={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
    stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
    runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.01;
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.01;
    runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
    for lnd=1:size(all_act{1},1)
        act=all_act{tnd}(lnd,:);
        [fb_av{lnd,tnd},pb_av{lnd,tnd},fb_snip{lnd,tnd},pb_snip{lnd,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
    end
end

uuu=[];
for tnd=1:size(all_act{1},1)
    for gnd=1:size(proj_meta(cur_site).rd,2)
        cur_ffm=shad_all{gnd}==3;
        if sum(order{gnd}==3)==1
            uuu(tnd,gnd)=corr((shad_all{gnd}==3)',all_act{gnd}(tnd,1:end-5000)')^2;
        else
            uuu(tnd,gnd)=corr((shad_all{gnd}==3)',all_act{gnd}(tnd,1:end-10000)')^2;
        end
    end
end

% ppp=diff(uuu,1,2);
% ppp=ppp>0.015;
% cells=find(sum(ppp(:,1:2),2)==2);
ppp=diff(uuu,1,2);
for gnd=1:size(proj_meta(cur_site).rd,2)-1
    ppp(:,gnd)=ppp(:,gnd)>0.01;
end
cells=find(sum(ppp(:,1:2),2)>=2);
rem=[];
for hnd=1:length(cells)
    for gnd=1:size(proj_meta(cur_site).rd,2)
        if ~isempty(fb_av{cells(hnd),gnd}{13})
            cur_fb_trace=fb_av{cells(hnd),gnd}{13};
            cur_pb_trace=pb_av{cells(hnd),gnd}{13};
            if mean(cur_fb_trace(15:18))*1.1<mean(cur_fb_trace(22:25))
                if abs(mean(cur_pb_trace(15:18))-mean(cur_pb_trace(22:25)))<0.05
                    rem(hnd,gnd)=0;
                else
                    rem(hnd,gnd)=1;
                end
            else
                rem(hnd,gnd)=1;
            end
        else
            rem(hnd,gnd)=0;
        end
    end
end
cells(sum(rem,2)==4)=[];

aaa=uuu(cells,:);

figure;
hold on
for gnd=1:size(proj_meta(cur_site).rd,2)
    scatter(ones(size(aaa,1),1)*gnd,aaa(:,gnd))
end
hold on
plot(mean(aaa),'k')

[~,~,ffm_snips,~]=meta_analysis_find_onset_snips(proj_meta,cur_site,vis_fl_all,order,exp_inds,shad_all);

ffm_resp={};
ffm_mean={};
for ind=1:length(cells)
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cur_act=all_act{tnd}(cells(ind),:);
        for hnd=1:size(ffm_snips{tnd},1)
            ffm_resp{ind,tnd}(hnd,:)=cur_act(ffm_snips{tnd}(hnd,1):ffm_snips{tnd}(hnd,2));
        end
        ffm_mean{tnd}(ind,:)=mean(ffm_resp{ind,tnd},1);
    end
end

maxi=0;
for gnd=1:size(proj_meta(cur_site).rd,2)
    maxi=max([maxi max(mean(ffm_mean{gnd},1)-min(mean(ffm_mean{gnd})))]);
end
maxi=maxi*1.1;
figure;
hold on
for gnd=1:size(proj_meta(cur_site).rd,2)
    subplot(1,size(proj_meta(cur_site).rd,2),gnd)
    plot(mean(ffm_mean{gnd},1)-min(mean(ffm_mean{gnd})))
    ylim([0 maxi])
    line([10 10],[0 maxi],'col','k')
end

figure;
for gnd=1:size(proj_meta(cur_site).rd,2)
    subplot(1,size(proj_meta(cur_site).rd,2),gnd)
    for tnd=1:length(cells)
        hold on
        plot(ffm_resp{tnd,gnd}','col',[0.8 0.8 0.8])
        ylim([0.95 3])
    end
    for tnd=1:length(cells)
        hold on
        if ~isempty(fb_av{tnd,gnd}{13})
            plot(fb_av{tnd,gnd}{13}(:,6:35),'linewidth',1.1)
            ylim([0.95 3])
        end
    end
end
cells